package com.itcast.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itcast.mapper.CategoryMapper;
import com.itcast.pojo.service.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubCategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据pid查询子分类
     */
    public List<Category> findSubCategory(Integer pid) {
        Category category = new Category();
        category.setPid(pid);
        QueryWrapper<Category> queryWrapper = Wrappers.query(category);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return categoryList;
    }
    /**
     * 条件查询
     */
    public List<Category> findSubCategoryByCodeOrName(String code, String name, Integer pid) {
        Category category = new Category();
        category.setCode(code);
        category.setName(name);
        category.setPid(pid);
        QueryWrapper<Category> queryWrapper = Wrappers.query(category);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return categoryList;
    }



    /**
     * 添加子分类
     * @param category
     */
    public void addSubCategory(Category category) {
        categoryMapper.insert(category);
    }

    /**
     * 删除子分类
     * @param id
     */
    public void deleteSubCategory(Integer id) {
        categoryMapper.deleteById(id);
    }

    /**
     * 修改子分类
     */
    public void updateSubCategory(Category category) {
        categoryMapper.updateById(category);
    }



}
